package com.wang.ymcc.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.wang.ymcc.dto.CourseDetailVo;
import com.wang.ymcc.dto.CourseOrderDaraVo;
import com.wang.ymcc.dto.CourseParamDto;
import com.wang.ymcc.dto.CourseStateVo;
import com.wang.ymcc.query.CourseQuery;
import com.wang.ymcc.service.ICourseService;
import com.wang.ymcc.domain.Course;
import com.wang.lwm.result.JSONResult;
import com.wang.lwm.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    @GetMapping("info/{ids}")
    public JSONResult info(@PathVariable String ids){
        CourseOrderDaraVo courseOrderDaraVo = courseService.info(ids);
        return JSONResult.success(courseOrderDaraVo);
    }
    /*

     */
    @GetMapping(value = "/courseState/{id}")
    public JSONResult courseStateById(@PathVariable Long id){
        CourseStateVo courseStateVo = courseService.courseStateById(id);
        return JSONResult.success(courseStateVo);
    }

    /*
        获取课程信息数据
     */
    @GetMapping("/detail/data/{id}")
    public JSONResult CourseDetailById(@PathVariable Long id){
        CourseDetailVo courseDetailVo =courseService.CourseDetailById(id);
        return JSONResult.success(courseDetailVo);
    }

    /*
        邀请二维码生成
     */
    @GetMapping("erweima")
    public JSONResult erweima() throws Exception{
        String code = courseService.erweima();
        return JSONResult.success(code);
    }

    @GetMapping("/getcode/{username}")
    public JSONResult getCode(@PathVariable("username") String username){
        String code = courseService.getCode(username);
        return JSONResult.success(code);
    }

    @PostMapping("/offLineCourse/{id}")
    public JSONResult offLineCourse(@PathVariable("id") Long id){
        courseService.offLineCourse(id);
        return JSONResult.success();
    }

    @GetMapping("/onLineCourse/{id}")
    public JSONResult save(@PathVariable("id") Long id){
        courseService.onLineCourse(id);
        return JSONResult.success();
    }

    @RequestMapping(value="/add",method= RequestMethod.POST)
    public JSONResult save(@RequestBody @Valid CourseParamDto course){
        courseService.save(course);
        return JSONResult.success();
    }

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Course course){
        if(course.getId()!=null){
            courseService.updateById(course);
        }else{
            courseService.insert(course);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        Wrapper<Course> wapper = new EntityWrapper<>();
        wapper.like("name",query.getKeyword());
        page = courseService.selectPage(page,wapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));

    }

}
